<template>
  <div class="goods-item" @click="itemClick">
    <img v-lazy="showImage" alt="" @load="imageLoad" :class="getImgHeight()">
    <div class="goods-info">
      <p class="title">{{goodsItem.title}}</p>
      <span class="price">¥{{goodsItem.price}}</span>
      <span class="collect">{{goodsItem.cfav}}</span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    goodsItem: {
      type: Object,
      default() {
        return {};
      }
    },
    imgHeight: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    showImage() {
      return this.goodsItem.image || this.goodsItem.img || this.goodsItem.show.img;
    }
  },
  methods: {
    imageLoad() {
      this.$bus.$emit('imageLoad');
    },
    itemClick() {
      if(this.goodsItem.iid) {
        this.$router.push('/detail/' + this.goodsItem.iid);
      } else {
        this.$toast("暂不支持查看推荐商品");
      }
    },
    getImgHeight() {
      return this.imgHeight ? ['other-height'] : ['default-height'];
    }
  }
}
</script>

<style scoped>
  .goods-item {
    width: 48%;
    padding-bottom: 10px;
  }

  .goods-item img {
    width: 100%;
    border-radius: 5px;
  }

  .goods-item img.default-height {
    height: 250px;
  }

  .goods-item img.other-height {
    height: 180px;
  }

  .goods-info {
    margin-top: 5px;
    font-size: 13px;
    text-align: center;
  }

  .goods-info p {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-bottom: 3px;
  }

  .goods-info .price {
    color: var(--color-high-text);
    margin-right: 20px;
  }

  .goods-info .collect {
    position: relative;
    padding-left: 15px;
  }

  .goods-info .collect::before {
    content: '';
    position: absolute;
    left: 0;
    top: -1px;
    width: 14px;
    height: 14px;
    background: url(~assets/img/common/collect.svg) 0 0/14px 14px no-repeat;
  }
</style>